import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
// import { AppController } from './app.controller';
// import { AppService } from './app.service';
import { AdminModule } from './admin/admin.module';
import { ApiModule } from './api/api.module';
import { SharedModule } from './shared/shared.module';
import { LoggerModule } from './logger/logger.module';
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';
import {
  AcceptLanguageResolver,
  CookieResolver,
  HeaderResolver,
  I18nModule,
  QueryResolver,
} from 'nestjs-i18n';
import * as path from 'path';
import methodOverride from 'src/shared/middlewares/method-override';
import { ServeStaticModule } from '@nestjs/serve-static';
import { EventEmitterModule } from '@nestjs/event-emitter';

const { combine, timestamp, printf } = winston.format;
@Module({
  imports: [
    // WinstonModule.forRoot({
    //   transports: [
    //     new winston.transports.Console({
    //       format: combine(
    //         timestamp({format:'YYYY-MM-DD hh:mm:ss'}),
    //         printf(({level, message, timestamp, context}) => {
    //           return `[Nest] ${process.pid}  - ${timestamp}     ${level} [${context}] ${message}`
    //         })
    //       )
    //     }),
    //     new winston.transports.File({
    //       filename: 'error.log',
    //       level: 'error'
    //     })
    //   ]
    // }),
    // LoggerModule,
    EventEmitterModule.forRoot({
      wildcard: true,
      delimiter: '.',
      global: true,
    }),
    ServeStaticModule.forRoot({
      rootPath: path.join(__dirname, '..', 'uploads'), // __dirname当前文件位置
      serveRoot: '/uploads',
    }),
    I18nModule.forRoot({
      fallbackLanguage: 'en', // 默认语言
      loaderOptions: {
        path: path.join(__dirname, '/i18n/'),
        watch: true,
      },
      resolvers: [
        new QueryResolver(['lang', 'l']), // url /app?lang=zh /app?l=zh
        AcceptLanguageResolver, // headers: {"accept-language":"zh"}
        new HeaderResolver(['x-custom-lang']), // headers: {"x-custom-lang": "zh"}
        new CookieResolver(), // cookie: x-custom-lang=zh
      ],
    }),
    AdminModule,
    ApiModule,
    SharedModule,
  ],
  controllers: [],
  providers: [],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(methodOverride).forRoutes('*');
  }
}
